package com.bwf.service;

import com.bwf.exception.RegistException;
import com.bwf.pojo.CreditAccount;
import com.bwf.pojo.SavingAccount;
import com.bwf.pojo.Account;
import com.bwf.util.DBUtil;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/11 9:37
 * @Copyright 博为峰
 */
public class Bank {

    public static ArrayList<SavingAccount> list = new ArrayList<>();

    static {
        try {
            list = new ArrayList<>();
            final File dir = new File("C:\\Users\\admin\\Desktop\\io");
            final File[] files = dir.listFiles();
            for (File file : files) {
                final ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
                final SavingAccount account = (SavingAccount) objectInputStream.readObject();
                list.add(account);
            }
            System.out.println(list);
        } catch (Exception e) {

        }
    }

    public Bank() {
    }

//    public Account verify(String cardNo, String password) {
//        SavingAccount savingAccount = new SavingAccount("110", "110", 110);
//        CreditAccount creditAccount = new CreditAccount("119", "119", 119);
//        if (savingAccount.getCardNo().equals(cardNo) && savingAccount.getPassword().equals(password)) {
//            return savingAccount;
//        } else if (creditAccount.getCardNo().equals(cardNo) && creditAccount.getPassword().equals(password)) {
//            return creditAccount;
//        }
//        return null;
//    }

//    public Account verify(String cardNo, String password) throws IOException, ClassNotFoundException {
//        final File dir = new File("C:\\Users\\admin\\Desktop\\io");
//        final File[] files = dir.listFiles(new FilenameFilterImpl(cardNo));
//        for (File file : files) {
//            final ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
//            final SavingAccount account = (SavingAccount) objectInputStream.readObject();
//            System.out.println(account.getPassword());
//        }
//        return null;
//    }

//    public Account verify(String cardNo, String password) throws IOException, ClassNotFoundException {
//        Account account = null;
//        Iterator<SavingAccount> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            SavingAccount savingAccount = iterator.next();
//            if (savingAccount.getCardNo().equals(cardNo) && savingAccount.getPassword().equals(password)) {
//                account = savingAccount;
//                break;
//            }
//        }
//        return account;
//    }

    public Account verify(String cardNo, String password) throws IOException, ClassNotFoundException, SQLException {
        Connection conn = DBUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("select * from t_account where acc_card_no = ? and acc_password = ?");
        preparedStatement.setString(1, cardNo);
        preparedStatement.setString(2, password);
        ResultSet resultSet = preparedStatement.executeQuery();
        SavingAccount savingAccount = null;
        while (resultSet.next()) {
            String accCardNo = resultSet.getString("acc_card_no");
            String accPassword = resultSet.getString("acc_password");
            double accBalance = resultSet.getDouble("acc_balance");
            savingAccount = new SavingAccount(accCardNo, accPassword, accBalance);
        }
        return savingAccount;
    }

    public void save(String cardNo, String password, double balance) throws SQLException {
        Connection conn = DBUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("update t_account set acc_balance = acc_balance + ? where acc_card_no = ?");
        preparedStatement.setDouble(1, balance);
        preparedStatement.setString(2, cardNo);
        int i = preparedStatement.executeUpdate();
    }

//    public void register(String cardNo, String password, double balance) {
//        SavingAccount savingAccount = new SavingAccount(cardNo, password, balance);
//        FileOutputStream fos = null;
//        ObjectOutputStream objectOutputStream = null;
//        try {
//            fos = new FileOutputStream("C:\\Users\\admin\\Desktop\\io\\" + cardNo + ".txt");
//            objectOutputStream = new ObjectOutputStream(fos);
//            objectOutputStream.writeObject(savingAccount);
//        } catch (IOException e) {
//            throw new RegistException("注册失败");
//        } finally {
//            try {
//                fos.close();
//                objectOutputStream.close();
//            } catch (IOException e) {
//            }
//        }
//    }

    public void register(String cardNo, String password, double balance) throws SQLException {
        Connection conn = DBUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("insert into t_account(acc_card_no,acc_password,acc_balance)values(?,?,?)");
        preparedStatement.setString(1, cardNo);
        preparedStatement.setString(2, password);
        preparedStatement.setDouble(3, balance);
        int i = preparedStatement.executeUpdate();
        System.out.println("i = " + i);
    }

    public SavingAccount query(String cardNo) {
        FileInputStream fis = null;
        ObjectInputStream objectInputStream = null;
        try {
            fis = new FileInputStream("C:\\Users\\admin\\Desktop\\io\\" + cardNo + ".txt");
            objectInputStream = new ObjectInputStream(fis);
            SavingAccount savingAccount = (SavingAccount) objectInputStream.readObject();
            return savingAccount;
        } catch (Exception e) {
            throw new RuntimeException("查询失败");
        } finally {
            try {
                fis.close();
                objectInputStream.close();
            } catch (IOException e) {
            }
        }
    }
}
